package com.haoshuang.zhouzhoubang.ui.fragment

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.FragmentMemberManagerBinding
import com.haoshuang.zhouzhoubang.model.bean.GroupBean
import com.haoshuang.zhouzhoubang.model.event.GetTeamMemberListEvent
import com.haoshuang.zhouzhoubang.ui.activity.PersonalHomeActivity
import com.haoshuang.zhouzhoubang.ui.activity.WorkerServiceRecordActivity
import com.haoshuang.zhouzhoubang.ui.adapter.Meger1Adapter
import com.haoshuang.zhouzhoubang.ui.vm.MemberManagerFViewModel
import com.jeremyliao.liveeventbus.LiveEventBus
import com.mufeng.libs.base.BaseVMFragment
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.vertical

/**
 *  成员管理片段
 */
class MemberManagerFragment : BaseVMFragment<MemberManagerFViewModel, FragmentMemberManagerBinding>() {

    companion object {
        fun getInstance(team_id: Int, group_status: Int, user_id: String): MemberManagerFragment {
            val fragment = MemberManagerFragment()
            fragment.arguments = Bundle().apply {
                putInt("team_id", team_id)
                putInt("group_status", group_status)
                putString("user_id", user_id)
            }
            return fragment
        }
    }

    private var team_id = 0
    private var group_status = 0
    private var user_id = ""

    private val data = arrayListOf<GroupBean.AllPeople>()
    private val adapter = Meger1Adapter(data)

    private var keywords = ""

    override fun initView(savedInstanceState: Bundle?) {
        if (arguments != null) {
            team_id = requireArguments().getInt("team_id", 0)
            group_status = requireArguments().getInt("group_status", 0)
            user_id = requireArguments().getString("user_id", "")
        }

        binding.recyclerView.vertical()
        binding.recyclerView.adapter = adapter

        adapter.setOnItemClickListener { _, view, position ->
            val item = adapter.getItem(position)
            startPage<PersonalHomeActivity>("user_id" to item?.uid)
        }

        adapter.setOnItemChildClick(id = R.id.tvServiceLog){view, position, item ->
            // 服务记录
            startPage<WorkerServiceRecordActivity>("user_id" to item?.uid.toString())
        }
        adapter.setOnItemChildClick(id = R.id.tvSde){view, position, item ->
            if (view.id== R.id.tvSde){
                if (item?.phone?.isBlank() == true) {
                    toast("电话不能为空")
                }
                val intent = Intent(Intent.ACTION_DIAL)
                intent.setData(Uri.parse("tel:${item?.phone}"));
                startActivity(intent)
            }
        }
    }

    override fun initData() {

        binding.refreshLayout.setOnRefreshListener {
            isShowing = false
            vm.getTeamMemberList()
        }

    }

    override fun lazyLoadData() {

    }

    override fun onResume() {
        super.onResume()
        vm.getTeamMemberList()
    }

    override fun startObserve() {

        vm.getTeamMemberListData.observeState(this){
            onLoading {showLoading()}
            onError {code, msg ->
                dismissLoading()
                binding.refreshLayout.finishRefresh()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                binding.refreshLayout.finishRefresh()
                data.clear()
                val list = it?.filter { it.nickname != null && it.nickname.contains(keywords) }?.toMutableList()
                data.addAll(list?: emptyList())
                LiveEventBus.get(GetTeamMemberListEvent::class.java).post(GetTeamMemberListEvent(data.size))

                adapter.notifyDataSetChanged()
            }
        }

    }

    fun search(content: String) {
        keywords = content
        vm.getTeamMemberList()
    }

}